package com.resou.reader.mine.vipcenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.MainActivity;
import com.resou.reader.R;
import com.resou.reader.api.entry.MonthlyVipBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.bookstore.more.MoreActivity;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.mine.vipcenter.adapter.MonthlyVipAdapter;
import com.resou.reader.mine.vipcenter.adapter.MonthlyVipGridAdapter;
import com.resou.reader.mine.vipcenter.callback.IMonthlyVipCallbackListener;
import com.resou.reader.mine.vipcenter.monthlybooks.MonthlyBooksActivity;
import com.resou.reader.mine.vipcenter.more.MonthlyMoreActivity;
import com.resou.reader.mine.vipcenter.vipCenter.VipCenter;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.eventbus.MessageEvent;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * VIP包月会员
 */
public class MonthlyVipActivity extends ResouBaseActivity<MonthlyVipPresenter> implements IMonthlyVipContract.IMonthlyVipView, OnRefreshListener {
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSend)
    TextView tvSend;
    @BindView(R.id.tvKT)
    TextView tvKT;
    @BindView(R.id.mTitleBar)
    RelativeLayout mTitleBar;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    List<MonthlyVipBean> list;
    @BindView(R.id.rlSubMonthly)
    RelativeLayout rlSubMonthly;//开通VIp

    /**
     * 启动VIP会员页面
     *
     * @param context 上下文
     */
    public static void start(Context context) {
        Intent starter = new Intent(context, MonthlyVipActivity.class);
        context.startActivity(starter);
    }

    /**
     * 启动VIP会员页面
     *
     * @param context 上下文
     */
    public static void start(Context context, boolean isToMain) {
        Intent starter = new Intent(context, MonthlyVipActivity.class);
        starter.putExtra("isToMain", isToMain);
        context.startActivity(starter);
    }


    MonthlyVipAdapter mMonthlyVipAdapter;
    boolean isToMain = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_monthly_vip;
    }

    @Override
    public void retryRefreshData() {
        mPresenter.refreshData();
    }

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        mPresenter = new MonthlyVipPresenter(this);
    }

    @Override
    protected void initView() {
        tvTitle.setText("包月VIP");
        list = new ArrayList<>();
        isToMain = getIntent().getBooleanExtra("isToMain", false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mMonthlyVipAdapter = new MonthlyVipAdapter(list);
        if (2 == UserInstance.getLoginData().getIsVipUser()) {
            tvKT.setText("续费包月VIP");
        } else {
            tvKT.setText("开通包月VIP");
        }
        ClassicsFooter.REFRESH_FOOTER_NOTHING = "别拉了，小热也是有底线的啦~";
        mMonthlyVipAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mRecyclerView.setAdapter(mMonthlyVipAdapter);
        mSmartRefreshLayout.setEnableFooterFollowWhenLoadFinished(true);
        mSmartRefreshLayout.setOnRefreshListener(this);
        mPresenter.refreshData();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mMonthlyVipAdapter.setiMonthlyVipCallbackListener(new IMonthlyVipCallbackListener() {
            @Override
            public void setIMonthlyVipMoreCallbackListener(String plateName, int plateId, int plateViewType, int position) {
                MonthlyMoreActivity.start(MonthlyVipActivity.this, plateName, plateId + "", plateViewType + "");
            }

            @Override
            public void setIMonthlyVipRefreshCallbackListener(MonthlyVipGridAdapter monthlyVipGridAdapter, int plateId, int plateViewType, int position) {
                if (isNetwork()) {
                    mPresenter.refreshModule(monthlyVipGridAdapter, plateId + "", plateViewType + "", 0, position);
                }
            }

            @Override
            public void setIMonthlyVipOpeningVip() {
                VipCenter.start(MonthlyVipActivity.this, false);
            }

            @Override
            public void setIMonthlyVipMonthlySub() {
                MonthlyBooksActivity.start(MonthlyVipActivity.this);
            }
        });
        /**
         * 控制开通会员的按钮显示与隐藏
         */
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (UserInstance.getLoginData().getIsVipUser() == 1) {
                    return;
                }
                if (!recyclerView.canScrollVertically(-1)) {
                    if (rlSubMonthly.getVisibility() == View.VISIBLE) {
                        rlSubMonthly.startAnimation(AnimationUtils.loadAnimation(MonthlyVipActivity.this, R.anim.fade_out));
                        rlSubMonthly.setVisibility(View.GONE);
                    }
                } else if (!recyclerView.canScrollVertically(1)) {
                    if (rlSubMonthly.getVisibility() == View.GONE) {
                        rlSubMonthly.startAnimation(AnimationUtils.loadAnimation(MonthlyVipActivity.this, R.anim.fade_in));
                        rlSubMonthly.setVisibility(View.VISIBLE);
                    }
                }
                if (dy > 0) {
                    if (rlSubMonthly.getVisibility() == View.GONE) {
                        rlSubMonthly.startAnimation(AnimationUtils.loadAnimation(MonthlyVipActivity.this, R.anim.fade_in));
                        rlSubMonthly.setVisibility(View.VISIBLE);
                    }
                } else if (dy < 0) {
                    if (rlSubMonthly.getVisibility() == View.VISIBLE) {
                        rlSubMonthly.startAnimation(AnimationUtils.loadAnimation(MonthlyVipActivity.this, R.anim.fade_out));
                        rlSubMonthly.setVisibility(View.GONE);
                    }
                }
            }
        });
        rlSubMonthly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VipCenter.start(MonthlyVipActivity.this, false);
            }
        });
    }


    @Override
    public void initData() {
    }

    @Override
    public void showContent() {

    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        dissLoading();
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {

    }


    @OnClick(R.id.ivBack)
    public void onViewClicked() {
        if (isToMain) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("index", 1);
            startActivity(intent);
        } else {
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isToMain) {
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra("index", 1);
                startActivity(intent);
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPresenter.refreshData();
    }

    /**
     * 充值成功后 重新刷新数据将用户状态更新
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        mPresenter.refreshData();
    }

    /**
     * 显示数据
     *
     * @param list
     */
    @Override
    public void showData(List<MonthlyVipBean> list) {
        mMonthlyVipAdapter.setNewData(list);
        mSmartRefreshLayout.finishRefresh();
        mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
        dissLoading();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
